package com.jmpt.fangbohui.adapter;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.google.gson.Gson;
import com.jmpt.fangbohui.Constants;
import com.jmpt.fangbohui.R;
import com.jmpt.fangbohui.bean.ManagerHouseBean.ho_houseList;
import com.jmpt.fangbohui.dao.LoginSQLiteOperator;
import com.jmpt.fangbohui.ui.personalcenter.AddBBClientActivity;
import com.jmpt.fangbohui.ui.personalcenter.EnterHouseActivity;
import com.jmpt.fangbohui.ui.personalcenter.ManageHouseActivity;
import com.jmpt.fangbohui.utils.DebugLog;
import com.jmpt.fangbohui.utils.ImageLoaderHelper;
import com.jmpt.fangbohui.utils.ToastHelper;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ManageHouseListAdapter extends BaseAdapter
{
	private Context mContext;
	private List<ho_houseList> mData;
	private boolean isDelete;//true 批量删除 false 单个删除
	private List<Integer> deleteList = new ArrayList<Integer>();
	private OnDeleteListCheckListener listener;
	private int currentOperatorPositon = 0;
	private ManageHouseActivity activity;
	private List<Integer> deletePosition = new ArrayList<Integer>();
	private List<Integer> publishPosition = new ArrayList<Integer>();
	private List<Integer> unpublishPosition = new ArrayList<Integer>();
	
	private ViewHolder holder;
	protected AlertDialog show;
	protected String current_houseid;
	
	public ManageHouseListAdapter(Context context,List<ho_houseList> mData,boolean isDelete){
		this.mContext = context;
		this.mData = mData;
		this.isDelete = isDelete;
		this.activity = (ManageHouseActivity) context;
	}
	@Override
	public int getCount()
	{
		return mData.size();
	}

	@Override
	public Object getItem(int position)
	{
		return mData.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent)
	{
		holder = null;
		if(convertView == null){
			holder = new ViewHolder();
			convertView = View.inflate(mContext, R.layout.item_manage_house, null);
			holder.iv_manage_house = (ImageView) convertView.findViewById(R.id.iv_manage_house);
			holder.tv_manage_house_title = (TextView) convertView.findViewById(R.id.tv_manage_house_title);
			holder.tv_manage_house_type = (TextView) convertView.findViewById(R.id.tv_manage_house_type);
			holder.tv_manage_house_mj = (TextView) convertView.findViewById(R.id.tv_manage_house_mj);
			holder.tv_manage_house_bj = (TextView) convertView.findViewById(R.id.tv_manage_house_bj);
			holder.tv_manage_house_price = (TextView) convertView.findViewById(R.id.tv_manage_house_price);
			holder.tv_manage_house_city = (TextView) convertView.findViewById(R.id.tv_manage_house_city);
			holder.tv_manage_house_street = (TextView) convertView.findViewById(R.id.tv_manage_house_street);
			holder.tv_manage_house_update = (TextView) convertView.findViewById(R.id.tv_manage_house_update);
			holder.tv_item_manage = (TextView) convertView.findViewById(R.id.tv_item_manage);
			holder.cb_delete_item_manage = (CheckBox) convertView.findViewById(R.id.cb_delete_item_manage);
			holder.rl_manage_house = (RelativeLayout) convertView.findViewById(R.id.rl_manage_house);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		ImageLoaderHelper.displayImage(mData.get(position).H_SmallPic, holder.iv_manage_house);
		holder.tv_manage_house_title.setText(mData.get(position).H_Title);
		String type  = "";
		if(mData.get(position).H_SaleOrRent.equals("1")){
			type = "出售";
		}else{
			type = "出租";
		}
		holder.tv_manage_house_type.setText(type);
		holder.tv_manage_house_mj.setText(mData.get(position).H_MJ+"㎡");
		holder.tv_manage_house_bj.setText(mData.get(position).H_Room+"房"+mData.get(position).H_Hall+"厅");
		holder.tv_manage_house_price.setText(mData.get(position).H_SalePrice+"元");
		holder.tv_manage_house_city.setText(mData.get(position).CityName);
		holder.tv_manage_house_street.setText(mData.get(position).ZoneName);
		holder.tv_manage_house_update.setText(mData.get(position).LastUpDate);
		String status = "";
		if(mData.get(position).status.equals("1")){
			status = "待发布";
		}else if(mData.get(position).status.equals("2")){
			status = "待审核";
		}else if(mData.get(position).status.equals("3")){
			status = "已发布";
		}else if(mData.get(position).status.equals("4")){
			status = "未通过";
		}
		holder.tv_item_manage.setText(status);

		if(isDelete){
			holder.cb_delete_item_manage.setVisibility(View.VISIBLE);
			holder.rl_manage_house.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					if(deletePosition.contains(position)){
						deletePosition.remove((Object)position);
						DebugLog.i("remove"+position);
						holder.cb_delete_item_manage.setChecked(false);
					}else{
						deletePosition.add(position);
						DebugLog.i("add"+position);
						holder.cb_delete_item_manage.setChecked(true);
					}
					ManageHouseListAdapter.this.notifyDataSetChanged();
				}
			});
		}else{
			holder.cb_delete_item_manage.setVisibility(View.GONE);
			holder.rl_manage_house.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					DebugLog.i("item click");
					current_houseid = mData.get(position).HouseID;
					AlertDialog.Builder builder = new Builder (mContext);
					View container = View.inflate(mContext, R.layout.dialog_manage_house, null);
					TextView tv_dialog_manage_house_edit = (TextView) container.findViewById(R.id.tv_dialog_manage_house_edit);
					TextView tv_dialog_manage_house_public = (TextView) container.findViewById(R.id.tv_dialog_manage_house_public);
					TextView tv_dialog_manage_house_unpublic = (TextView) container.findViewById(R.id.tv_dialog_manage_house_unpublic);
					TextView tv_dialog_manage_house_refresh = (TextView) container.findViewById(R.id.tv_dialog_manage_house_refresh);
//					operatorHouse();
					tv_dialog_manage_house_edit.setOnClickListener(new OnClickListener()
					{
						@Override
						public void onClick(View v)
						{
							show.dismiss();
							Intent intent = new Intent(mContext,EnterHouseActivity.class);
							intent.putExtra("HouseID", current_houseid);
							mContext.startActivity(intent);
						}
					});
					tv_dialog_manage_house_public.setOnClickListener(new OnClickListener()
					{
						@Override
						public void onClick(View v)
						{
							show.dismiss();
							activity.operatorHouse(current_houseid, Constants.URL.PUBLISH);
						}
					});
					tv_dialog_manage_house_unpublic.setOnClickListener(new OnClickListener()
					{
						@Override
						public void onClick(View v)
						{
							show.dismiss();
							activity.operatorHouse(current_houseid, Constants.URL.UNPUBLISH);
						}
					});
					tv_dialog_manage_house_refresh.setOnClickListener(new OnClickListener()
					{
						@Override
						public void onClick(View v)
						{
							show.dismiss();
							activity.operatorHouse(current_houseid, Constants.URL.REFRESH);
						}
					});
					
					builder.setView(container);
					builder.setCancelable(true);
					show = builder.show();
				}
			});
		}
		if(deletePosition.contains(position)){
			holder.cb_delete_item_manage.setChecked(true);
		}else{
			holder.cb_delete_item_manage.setChecked(false);
		}
		return convertView;
	}
//	protected void changeHouse(int position) {
//		Intent intent = new Intent(mContext,EnterHouseActivity.class);
//		intent.putExtra("HouseID", mData.get(position).HouseID);
//		mContext.startActivity(intent);
//	}
	public String showDeletePosition(){
		String houseIDs = "";
		for (int element : deletePosition) {
			houseIDs+=mData.get(element).HouseID+",";
		}
		if(!TextUtils.isEmpty(houseIDs)){
			houseIDs = houseIDs.substring(0, houseIDs.length()-1);
		}
		return houseIDs;
	}
	protected void HttpDelete(final String houseID,final int position) {
		
	}
	/**
	 * 
	 * setDeleteStatus:设置删除状态、是批量删除还是单个删除
	 *
	 * @author Lee
	 * @since JDK 1.7
	 */
	public void setDeleteStatus(){
		deletePosition.clear();
		isDelete = !isDelete;
		this.notifyDataSetChanged();
	}
	public class ViewHolder{
		public RelativeLayout rl_manage_house;
		public ImageView iv_manage_house;
		public TextView tv_manage_house_title;
		public TextView tv_manage_house_type;
		public TextView tv_manage_house_mj;
		public TextView tv_manage_house_bj;
		public TextView tv_manage_house_price;
		public TextView tv_manage_house_city;
		public TextView tv_manage_house_street;
		public TextView tv_manage_house_update;
		public TextView tv_item_manage;
		public CheckBox cb_delete_item_manage;
	}
	public interface OnDeleteListCheckListener{
		void getDeleteList(List<Integer> deleteList);
	}
	public void setOnDeleteListCheckListener(OnDeleteListCheckListener listener){
		this.listener = listener;
	}
}
